﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ycdlib
{
	/// <summary>
	/// 文字列のYCNode
	/// </summary>
	internal class YCNodeString : YCNodeBase
	{
		#region フィールド

		private string val;

		#endregion


		#region YCNode メンバ

		/// <summary>
		/// YC型の名前は“int”
		/// </summary>
		public override string YCType
		{
			get { return "string"; }
			internal set { throw new NotSupportedException(); }
		}

		/// <summary>
		/// 値を取得、または設定できる
		/// </summary>
		public override object Value
		{
			get
			{
				return val;
			}
			set
			{
				val = (string)value;
			}
		}




		/// <summary>
		/// 子供の数は常にゼロ
		/// </summary>
		public override int ChildCount
		{
			get { return 0; }
		}


		public override void Write ( System.IO.Stream s, int p )
		{
			Encoding u = Encoding.UTF8;

			StringBuilder sb = new StringBuilder();
			while( p > 0 )
			{
				sb.Append( "	" );
				--p;
			}
			sb.Append( "string " )
				.Append( Name );
			if( !string.IsNullOrEmpty(ID) )
				sb.Append( ":" ).Append( ID );
			sb.Append( "(\"" )
				.Append( val.Replace( "\\", "\\\\" ).Replace( "\"", "\\\"") )
				.Append( "\")\n" );

			byte[] buff = u.GetBytes( sb.ToString() );
			s.Write( buff, 0, buff.Length );
		}


		/// <summary>
		/// 子供を持たないため、このメソッドはサポートされません
		/// </summary>
		/// <param name="newNode"></param>
		public override void AddChild ( YCNode newNode )
		{
			throw new NotSupportedException();
		}



		#endregion


		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="p"></param>
		public YCNodeString ( YCNode p ) : base(p)
		{
		}


		static public YCNode CreateNode ( YCNode p ) { return new YCNodeString( p ); }
/*
		static public YCNode Parse ( YCNode parentNode, string type, string name, string id, Parser p )
		{
			YCNodeString __res = new YCNodeString( parentNode );
			__res.Name = name;
			__res.ID = id;


			//	値を読み込む
			string o = p.readString();
System.Diagnostics.Debug.WriteLine("value = " + o.ToString() );

			__res.Value = o;

			return __res;
		}
*/
	}
}
